package io.renren.modules.yst.controller;

import com.aliyun.oss.common.utils.DateUtil;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.R;
import io.renren.modules.yst.entity.WorkerCardEntity;
import io.renren.modules.yst.entity.WorkerEntity;
import io.renren.modules.yst.form.CheckinForm;
import io.renren.modules.yst.form.ManagerLoginForm;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.SysUserTokenService;
import io.renren.modules.yst.service.WorkerCardRecordService;
import io.renren.modules.yst.service.WorkerCardService;
import io.renren.modules.yst.service.WorkerService;
import io.renren.modules.yst.vo.RecordByCarVo;
import io.renren.modules.yst.vo.RecordByDateVo;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("yst/manager")
public class ManagerController extends AbstractController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserTokenService sysUserTokenService;
    @Autowired
    private WorkerCardRecordService workerCardRecordService;
    @Autowired
    private WorkerCardService workerCardService;
    @Autowired
    private WorkerService workerService;



    private static final String GROUP_BY = "groupby";
    private static final String START_DATE = "startDate";
    private static final String END_DATE = "endDate";




    /**
     * 登录
     */
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody ManagerLoginForm form)throws IOException {


        //用户信息
        SysUserEntity user = sysUserService.queryByUserName(form.getUsername());

        //账号不存在、密码错误
        if(user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }

        //账号锁定
        if(user.getStatus() == 0){
            return R.error("账号已被锁定,请联系管理员");
        }

        //生成token，并保存到数据库
        R r = sysUserTokenService.createToken(user.getUserId());
        return r;
    }


    /**
     * 退出
     */
    @PostMapping("logout")
    public R logout() {
        sysUserTokenService.logout(getUserId());
        return R.ok();
    }


    /**
     *
     */
    @GetMapping("/getcountbydate")
    public R getCountByDate( @RequestParam Map<String, Object> params) {

        //
        String type= (String)params.get(ManagerController.GROUP_BY);
        String format="";
        String startDate = (String)params.get(ManagerController.START_DATE);
        String endDate = (String)params.get(ManagerController.END_DATE);
        LocalDate localStartDate=null;
        LocalDate localEndDate=null;
        if(StringUtils.isEmpty(startDate)){

            startDate= LocalDate.now().dayOfMonth().withMinimumValue().toString();
        }else{

        }

        if(StringUtils.isEmpty(endDate)){

            endDate= LocalDate.now().dayOfMonth().withMaximumValue().toString();
        }

        if(StringUtils.isEmpty(type)||"DAY".equals(type)){

            type="DAY";
            format="%Y-%c-%d";
        }else{

            if("MONTH".equals(type)){
                format="%Y-%c";
            }
            else{
                format="%Y";
            }
        }







        List<RecordByDateVo> result = workerCardRecordService.queryCountByDate(format,startDate, endDate);

        //返回榜单id
        return R.ok().put(ManagerController.START_DATE,startDate).put(ManagerController.END_DATE,endDate).put("list",result);
    }


    /**
     *
     */
    @GetMapping("/getcountbycar")
    public R getCountByCar( @RequestParam Map<String, Object> params) {

        //
        String startDate = (String)params.get(ManagerController.START_DATE);
        String endDate = (String)params.get(ManagerController.END_DATE);

        if(StringUtils.isEmpty(startDate)){

            startDate= LocalDate.now().dayOfMonth().withMinimumValue().toString();
        }

        if(StringUtils.isEmpty(endDate)){

            endDate= LocalDate.now().dayOfMonth().withMaximumValue().toString();
        }


        List<RecordByCarVo> result = workerCardRecordService.queryCountByCarNumber(startDate, endDate);

        //返回榜单id
        return R.ok().put(ManagerController.START_DATE,startDate).put(ManagerController.END_DATE,endDate).put("list",result);
    }




    /**
     *
     */
    @GetMapping("/getcardInfo/{id}")
    public R getCardInfo( @PathVariable("id") Long id) {
        //查询卡
        WorkerCardEntity workerCard = workerCardService.queryById(id);
        WorkerEntity workerEntity= workerService.getById(workerCard.getWorkerId());

        //返回榜单id
        return R.ok().put("workerCard",workerCard).put("worker",workerEntity);
    }

    /**
     *
     */
    @PostMapping("checkin")
    public R checkin( @RequestBody CheckinForm form) {
        //查询卡
        WorkerCardEntity workerCard = workerCardService.queryById(form.getCardId());
        //获取自己的用户
        workerCardRecordService.create(workerCard,getUser());
        //返回榜单id
        return R.ok();
    }



}
